<template>
  <div class="app-container">

    <!-- 批量导入 -->
    <div class="filter-container">
      <el-form ref="form" label-width="80px">
        <el-upload
          ref="upload"
          :auto-upload="true"
          :multiple="false"
          :show-file-list="false"
          :with-credentials = "true"
          :before-upload = "upload"
          :action="uploadUrl"
          accept=".xls"
          class="filter-item">
          <el-button slot="trigger" type="primary" icon="el-icon-upload" >批量导入</el-button>
          <div slot="tip" class="el-upload__tip"><span style="font-size: small; color: orangered; ">教务系统导出的《学生基本信息.xls》一定要先另存为xls文件，切记！</span></div>
        </el-upload>
      </el-form>
      <hr>
    </div>

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.keyWord" clearable class="filter-item" style="width: 200px; margin-right: 10px" placeholder="请输入查询词" @keyup.enter.native="handleFilter"/>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" row-key="id" style="width: 100%;margin-bottom: 20px;" border>
      <el-table-column label="学号" prop="sno"/>
      <el-table-column label="姓名" prop="name"/>
      <el-table-column label="所属院系" prop="department"/>
      <el-table-column label="专业" prop="major"/>
      <el-table-column label="行政班名" prop="className"/>
      <el-table-column label="证件号" prop="idCardNumber"/>
      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageIndex" :limit.sync="listQuery.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import administrationApi from '@/api/administration'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'Student',
  components: { Pagination },
  data () {
    return {
      list: [],
      total: 0,
      listQuery: {
        pageIndex: 1,
        pageSize: 20,
        keyWord: undefined
      },
      listLoading: true,
      uploadUrl: '/api/admin/student/upload'
    }
  },
  created () {
    this.getList()
  },
  methods: {
    getList () {
      this.listLoading = true
      administrationApi.listStudent(this.listQuery).then(response => {
        this.list = response.response.list
        this.total = response.response.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleFilter () {
      this.listQuery.pageIndex = 1
      this.getList()
    },
    handleCreate () {
      this.listQuery.pageIndex = 1
      this.getList()
    },
    upload (file) {
      this.listLoading = true
      return new Promise((resolve, reject) => {
        administrationApi.uploadFile(this.uploadUrl, file).then(response => {
          this.$message.success('添加成功')
          this.getList()
          reject(new Error('更新成功'))
        }).catch((error) => {
          console.log(error)
          this.listLoading = false
          this.$message.error('添加失败')
          reject(new Error('更新失败'))
        })
      })
    },
    handleDelete (row) {
      let _this = this
      administrationApi.deleteStudent(row.id).then(re => {
        if (re.code === 1) {
          _this.getList()
          _this.$message.success(re.message)
        } else {
          _this.$message.error(re.message)
        }
      })
    }
  }
}
</script>
